var slide_info=[];
var audio_info={};

var cmp_info={};

var view_component='audio-slideshow';
var view_container='audio-slides';
var currentSlide=-1;
var jPlayerPath='/lib/swf';
var suppliedFileType='mp3';
var playSelector='.audio-play';
var pauseSelector='.audio-pause';
var currentTimeSelector='.play-time';
var durationSelector='.total-time';
var playheadSelector='.playhead';
var timelineSelector='.timeline';
var isPlaying=false;
var $slides;

var interval_zoomin=0;
/*
var zoom_info={};
*/

(function($){
	$.fn.slide_obj=function(options){
        var settings=$.extend({
        	'class':'slide_obj',
        	'type':'',
            'id':'',
            'timeslot':0,
            'tag':'',
            'thumb':'',
            'image':'',
            'subdata':'',
            },options);
        
        this.get_info=function(s_id){
        	return settings[s_id];
        };
        
    };
	
	$.fn.preview2=function(options){
		var settings=$.extend({
			'interval-zoomin':500,
		},options);
		
		interval_zoomin=settings['interval-zoomin'];
		
		cmp_info={
			'w':$('#'+view_container).width(),
			'h':$('#'+view_container).height(),
			'rate':$('#'+view_container).width()/
				$('#'+view_container).height(),
		};
		
		return this.each(function(){
			contact_session(TYPE_RETRIEVE_SLIDES,null,this);
		});
		
	};
}(jQuery));

function setup_components(cmp){

	var $that=$(cmp),
		$currentTime=$that.find(currentTimeSelector),
		$duration=$that.find(durationSelector),
		$playhead=$that.find(playheadSelector),
		$timeline=$that.find(timelineSelector),
		$playButton=$that.find(playSelector),
		$pauseButton=$that.find(pauseSelector),
		slideTimes=new Array(),
		audioDurationinSeconds=
			parseInt($that.attr('data-audio-duration'));		

	$pauseButton.hide();

	$slides=$that.find('#'+view_container).children();
	var slidesCount=$slides.length;

	var id='',second=0,thumb='';
	var img,$marker,l;
	for (var i=0;i<slide_info.length;i++){
		id=slide_info[i].get_info('id');
		$('#'+id).hide();
		
		if (i>0){
			second=parseInt(slide_info[i].get_info('timeslot'));
			thumb=slide_info[i].get_info('thumb');
			slideTimes.push(second);
			
			img='<span><img src="'+thumb+'"></span>';
			$marker=$('<a class="marker" data-time="'+second+'">'+
					img+'</a>');
			l=(second/audioDurationinSeconds)*$that.width();
			$marker.css('left',l).click(function(e){
				$jPlayerObj.jPlayer('play',
						parseInt($(this).attr('data-time'))+.5);
			});

			$timeline.append($marker);
		}
	}
	
	/*
	$slides.each(function(index,el){
		var $el=$(el);
		$el.hide();
		
		id=$el.attr('id');
		second=parseInt($el.attr('timeslot'));
		thumb=$el.attr('thumb');
		
		if(index>0){
			slideTimes.push(second);
		
			var img='<span><img src="'+thumb+'"></span>',
				$marker=$('<a href="javascript:;" class="marker" data-time="'+second+'">'+img+'</a>'),
				l=(second/audioDurationinSeconds)*$that.width();
			$marker.css('left',l).click(function(e){
				$jPlayerObj.jPlayer('play',parseInt($(this).attr('data-time'))+.5);
			});

			$timeline.append($marker);
		}
	});
	*/

	var $jPlayerObj=$('<div></div>');
	$that.append($jPlayerObj);

	$jPlayerObj.jPlayer({
		ready:function(){
			$.jPlayer.timeFormat.padMin=false;
			$(this).jPlayer('setMedia',{
				mp3:$that.attr('data-audio'),
			});
		},
		swfPath:jPlayerPath,
		supplied:suppliedFileType,
		preload:'auto',
		cssSelectorAncestor:'',
	});
		
	/** time-update event */
	$jPlayerObj.bind($.jPlayer.event.timeupdate,function(event){
		var curTime=event.jPlayer.status.currentTime;
		audioDurationinSeconds=event.jPlayer.status.duration;
		var p=(curTime/audioDurationinSeconds)*100+'%';

		$currentTime.text($.jPlayer.convertTime(curTime));
		$duration.text($.jPlayer.convertTime(audioDurationinSeconds));

		$playhead.width(p);

		if(slidesCount){
			var nxtSlide=0;
			for(var i=0;i<slidesCount;i++){
				if(slideTimes[i]<curTime){
					nxtSlide=i+1;
				}
			}

			setAudioSlide(nxtSlide);
			
		}
	});
		
	/** play event */
	$jPlayerObj.bind($.jPlayer.event.play,function(event){
		isPlaying=true;
		$playButton.hide();
		$pauseButton.show();
	});
	
	/** pause event */
	$jPlayerObj.bind($.jPlayer.event.pause,function(event){
		isPlaying=false;
		$pauseButton.hide();
		$playButton.show();
	});
	
	$slides.click(function(event){
		$jPlayerObj.jPlayer('play');
	});
	
	$playButton.click(function(event){
		$jPlayerObj.jPlayer('play');
	});
		
	$pauseButton.click(function(event){
		$jPlayerObj.jPlayer('pause');
	});
	
	$timeline.click(function(event){
		var l=event.pageX-$(this).offset().left;
		var t=(l/$that.width())*audioDurationinSeconds;

		$jPlayerObj.jPlayer('play',t);
	});
	
	setAudioSlide(0);
	

}

/*
function zoom_in(){
	if (isPlaying){
		img=zoom_info['zoom-img'];
		var cnt=zoom_info['zoom-count'];
		var w=cmp_info['w'],
			h=cmp_info['h'],
			t=img.css('margin-top'),
			l=img.css('margin-left');
		img.css({
			'width':w+cnt+2,
			'height':h+cnt+2,
			'margin-top':t-1,
			'margin-left':l-1,
		});
		zoom_info['zoom-count']++;
	}
}
*/

function setAudioSlide(n){
	if(n != currentSlide){
		
		if($slides.get(currentSlide)){
			$($slides.get(currentSlide)).fadeOut();
		}

		$($slides.get(n)).fadeIn();
		currentSlide = n;
		
		/*
		if (isPlaying){
			if (zoom_info['zoom-handle']!=null){
				clearInterval(zoom_info['zoom-handle']);
			}
			
			zoom_info={
				'zoom-count':0,
				'zoom-img':$($slides.get(currentSlide)).find('img'),
				'zoom-handle':setInterval(zoom_in,interval_zoomin),
			};
		}
		*/
	}
}

function contact_session(type,list,component){
	var ctype='retrieve-slides';
	if (type==TYPE_RETRIEVE){
		ctype='retrieve';
	}else if (type==TYPE_RETRIEVE_SLIDES){
		ctype='retrieve-slides';
	}
	
	slide_idx=0;
	$('#'+view_container).html('');
	
	$.ajax({
		url: './jsp/modules/session.jsp',
		data: {
			'type':ctype,
			'list':list,
			'id':0,
			'content':''
		},
		   
		type: 'post',
		success:function(data){
			var arr=jQuery.parseJSON(data);
			var e=null,k=null,v=null;
			var idx=0;
			$.each(arr,function(){
				e=this.split(server_splitter);
				k=e[0];
				v=e[1];
				if (k.indexOf('audio') >=0){
					add_audio(k,v);
				}else{
					add_slide(k,v,idx);
					slide_idx++;
				}
				idx++;
			});

			setup_components(component);
		},		
	});
}

function add_slide(id,data,idx){
	var e=$(data).find('input').val().
				trim().split(splitter);
	var type=e[0];
	var thumb=e[1];
	var img=e[2];
	var f=e[3].split(data_splitter),
		img_w=parseInt(f[0]),
		img_h=parseInt(f[1]);
	var timeslot=e[4]!=undefined?parseInt(e[4]):0;
	
	slide_info[idx]=new $.fn.slide_obj({
		'type':type,
        'id':id,
        'timeslot':timeslot,
        'tag':'',
        'thumb':thumb,
        'image':img,
        'subdata':e[3],
		
	});
	
	var rate=img_w/img_h,
		srate=cmp_info['rate'];
	var w,h,l,t;
	if (rate>srate){
		h=parseInt(cmp_info['h']);
		w=h*srate;
		l=parseInt(-(w-img_w)/2);
		t=0;
	}else{
		w=parseInt(cmp_info['w']);
		h=w/srate;
		l=0;
		t=parseInt(-(h-img_h)/2);
	}
	
	$('#'+view_container).append(''+
		'<div class="div_slide">'+
		'	<div style="position:absolute;"'+
		'				width:'+w+'px;height:'+h+'px;'+
		'				margin-left:'+l+'px;margin-top:'+t+'px;">'+
		'		<img src="'+img+'" '+
		'			style="width:'+w+'px;'+
		'				height:'+h+'px;'+
		'				margin-left:0px;margin-top:0px;">'+
		'	</div>'+
		'</div>');
	
	/*
	if (type==TYPE_IMAGE_SLIDE){
		slideContent=get_image_slide(id,data);
	}else if (type==TYPE_U2B_SLIDE){
		slideContent=get_u2b_slide(id,data);
	}else if (type==TYPE_ARTICLE_SLIDE){
		slideContent=get_article_slide(id, data);
	}else if (type==TYPE_FB_SLIDE){
		
	}
	*/
	
}

function add_audio(){
	
}